package com.pan.admin.module.loanConfig.service.impl;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.module.channel.dao.LoanChannelFormFieldDao;
import com.pan.admin.module.channel.entity.LoanChannelFormField;
import com.pan.admin.module.channel.entity.vo.ElementLabelVO;
import com.pan.admin.module.channel.entity.vo.ElementTreeVO;
import com.pan.admin.module.loanConfig.dao.LoanFormFieldDao;
import com.pan.admin.module.loanConfig.entity.LoanFormField;
import com.pan.admin.module.loanConfig.service.LoanFormFieldService;
import com.pan.admin.module.loanProduct.dao.LoanProductPlanFieldDao;
import com.pan.admin.module.loanProduct.entity.LoanProductPlanField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 动态单字段Service业务层处理
 *
 * @author ghb
 * @date 2023-09-11
 */
@Service
public class LoanFormFieldServiceImpl implements LoanFormFieldService {
    @Autowired
    private LoanFormFieldDao loanFormFieldDao;

    @Autowired
    private LoanProductPlanFieldDao loanProductPlanFieldDao;

    @Autowired
    private LoanChannelFormFieldDao loanChannelFormFieldDao;

    /**
     * 根据ID获取列表动态单字段
     *
     * @param id 动态单字段主键
     * @return 动态单字段
     */
    @Override
    public LoanFormField getById(Long id) {
        return loanFormFieldDao.getById(id);
    }

    /**
     * 查询动态单字段列表
     *
     * @param loanFormField 动态单字段
     * @return 动态单字段
     */
    @Override
    public List<LoanFormField> list(LoanFormField loanFormField) {
        return loanFormFieldDao.list(loanFormField);
    }

    /**
     * 统计  机构表单关联数据
     *
     * @param loanFormField 动态单字段
     * @return 动态单字段
     */
    @Override
    public int countFormProduct(LoanProductPlanField loanFormField) {
        return loanProductPlanFieldDao.count(loanFormField);
    }

    /**
     * 统计  渠道表单关联数据
     *
     * @param loanFormField 动态单字段
     * @return 动态单字段
     */
    @Override
    public int countFormChannel(LoanChannelFormField loanFormField) {
        return loanChannelFormFieldDao.count(loanFormField);
    }

    /**
     * 查询渠道关联列表的树形控件渲染
     */
    @Override
    public List<ElementTreeVO> treeList(LoanFormField loanFormField) {
        List<LoanFormField> list = loanFormFieldDao.list(loanFormField);
        List<ElementTreeVO> treeVOList = new ArrayList<>();

        if (!ValidParam.isEmpty(list)) {
            ElementTreeVO treeVO = new ElementTreeVO();
            List<ElementLabelVO> labelVOList = new ArrayList<>();

            for (LoanFormField field : list) {
                ElementLabelVO labelVO = new ElementLabelVO();
                labelVO.setLabel(field.getFieldName());
                labelVO.setValue(field.getId());
                labelVOList.add(labelVO);
            }

            treeVO.setList(labelVOList);
            treeVO.setLabel("全选");
            treeVO.setValue(0L);
            treeVOList.add(treeVO);
        }
        return treeVOList;
    }

    /**
     * 新增动态单字段
     *
     * @param loanFormField 动态单字段
     * @return 结果
     */
    @Override
    public int insertLoanFormField(LoanFormField loanFormField) {
        loanFormField.setCreateTime(DateUtils.getNowDate());
        loanFormField.setStatus(1);
        loanFormField.setCreateBy(SecurityUtils.getUsername());
        return loanFormFieldDao.insertLoanFormField(loanFormField);
    }

    /**
     * 修改动态单字段
     *
     * @param loanFormField 动态单字段
     * @return 结果
     */
    @Override
    public int updateLoanFormField(LoanFormField loanFormField) {
        loanFormField.setUpdateTime(DateUtils.getNowDate());
        loanFormField.setUpdateBy(SecurityUtils.getUsername());
        return loanFormFieldDao.updateLoanFormField(loanFormField);
    }

}
